<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">商品销售记录</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="商品名字">
        <el-input v-model="queryForm.saleGoodsName" placeholder="商品名字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button type="primary" @click="initCharts()">数据柱状图</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="100%" >
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="saleId" label="编号" style="0%"></el-table-column>
      <el-table-column prop="saleGoodsName" label="商品名字" style="0%"></el-table-column>
      <el-table-column prop="saleGoodsNums" label="商品销量" style="0%"></el-table-column>
      <el-table-column prop="saleGoodsPrice" label="商品单价" style="0%"></el-table-column>
      <el-table-column prop="saleGoodsPriceCount" label="商品总价" style="0%"></el-table-column>
      <el-table-column prop="saleGoodsTime" label="卖出日期" style="0%"></el-table-column>
      <el-table-column prop="saleBuyTele" label="买家电话" style="0%"></el-table-column>
      <el-table-column prop="saleBuyEmail" label="买家邮件" style="0%"></el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >
    </el-pagination>

    <el-dialog :title="title" :visible.sync="isShow" width="50%">
      <div>
    		<div style="width:500px;height:500px" ref="chart"></div>
      </div>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  const echarts = require('echarts');
  export default {
      data() {
        return {
          title:"数据柱状图",
          isShow:false,
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          queryForm: {
            page: 1,
            row: 10,
            saleGoodsName:"",
          },
          option:{
            title: { text: '商品销售记录' },
            tooltip: {},
            xAxis: {
            data:[]
            },
            yAxis: {},
            series: [{
            name: '销量',
            type: 'bar',
            data:[]
            }]
          }
        }
      },
      methods: {
        loadData() { //加载数据
          var self = this;
          var http = this.$http.post("/api/sale/saleIndex",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              console.log(rs.data.data.data)
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
              for(var i = 0;i < rs.data.data.data.length;i++){
                self.option.xAxis.data[i] = rs.data.data.data[i].saleGoodsName;
                self.option.series[0].data[i] = rs.data.data.data[i].saleGoodsNums;
              }
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        initCharts(){
            this.isShow = true;
        		// 基于准备好的dom，初始化echarts实例
        		let myChart = echarts.init(this.$refs.chart);
        		// 绘制图表
            myChart.setOption(this.option)
        		}
      },
      mounted(){
        this.loadData();
      }
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
